/// <reference path="tools.js" />
/// <reference path="/jquery-easyui-1.4.3/jquery.easyui.min.js" />
var kjTime = 360000000;//开奖时间毫秒
var tzTime = 360000000;//投注时间毫秒
var ztTime = 5000;//投注结束后暂停时间
var ekjTime = 20000;//投注结束后开奖时间
var pjTime = 5000;//投注结束后派奖时间
var lastTime = new Date();
var lastIssue = null;//上次下注期次
var curIssue = null;//当前下注期次
var curKJIssue;//当前开奖期次
var lastLotteryNum = "0";
var msg = "";
var pageid = 0;
function SetUserInfo(user) {
      $(".js-username").html("账户："+user.UserName);
	  //(user.sumMoney>0 ? '<span style="color:#FFE48D;">'+user.sumMoney+'(盈)</span>' : '<span style="color:red;">'+user.sumMoney+'(亏)</span>')
      $(".js-usermoney").html("余额：" + user.UserMoney);
      $(".js-usertype").html(user.UserType);
	  if(user.sumMoney>0){
		  $(".js-usersummoney").html('战绩：<span style="color:#FFE48D;">'+user.sumMoney+'(盈)</span>');
      }else if (user.sumMoney<0){
		  $(".js-usersummoney").html('战绩：<span style="color:red;">'+user.sumMoney+'(亏)</span>');
      }else{
          $(".js-usersummoney").html('战绩：0');
      }
    //$(".js-usersummoney").html("战绩：" + user.sumMoney);
}
//获取总投注
function GetAllBet(tag) {
    $.ajax({
        url: "/Uc/GetAllBet?" + Math.random(),
        type: "post",
        dataType: "json",
        success: function (result) {
            if (!result.success) {
                //console.error(result.msg);
                $.messager.alert("提示", result.msg);
                setTimeout('window.location.href = "/Home"', 2000);
                return;
            } else {
                $(".js-allbet-z").text(result.list.hongtl);
                $(".js-allbet-f").text(result.list.dfw);
            }
            if (tag!=true)
                setTimeout(GetAllBet, 3000);
        },
        complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
            if (status == 'timeout') {//超时,status还有success,error等值的情况
                if (tag != true)
                    setTimeout(GetAllBet, 3000);
            }
        }
    });
}
//刷新用户信息
function GetUserInfo() {
    $.ajax({
        url: "/Uc/GetUserInfo?" + Math.random(),
        type: "post",
        dataType: "json",
        success: function (result) {
            if (!result.success) {
                $.messager.alert("提示", result.msg);
                setTimeout('window.location.href = "/Home"', 2000);
                return;
            } else {
                SetUserInfo(result.list);
            }
        }
    });
}

function CancelBet() {
    $.messager.alert("提示", msg);
    return true;
}

//获取当前下注期号
function GetIssue() {
    $.ajax({
        url: "/Uc/GetIssue?" + Math.random(),
        type: "post",
        dataType: "json",
        success: function (result) {
            if (!result.success) {
                $("#SubBet").attr("onclick", "CancelBet();");
                $(".js-fastbet").attr("onclick", "CancelBet();");
                //$.messager.alert("提示", result.msg);
                //setTimeout('window.location.href = "/Home"', 2000);
                setTimeout(GetIssue, 1000);
            } else {
                curIssue = result.list;
                //if (lastIssue != null && pageid==1)
                //    $.messager.show({
                //        title: '提示',
                //        msg: "第" + lastIssue.IssueNum + "期已停止投注，当前投注期号：" + curIssue.IssueNum,
                //        showType: 'fade',
                //        style: {
                //            right: '',
                //            bottom: ''
                //        }
                //    });
                tzTime = result.nTime;

                //$("#BetInfo").html("<b id=\"sscIssue\">" + curIssue.IssueNum + "</b>正在投注<br>剩余时间<strong name=\"countDown\">" + SecondsToStr(tzTime / 1000) + "</strong>");
                $("#BetInfo").html("第<b id=\"sscIssue\">" + curIssue.IssueNum + "</b>期正在投注, 剩余时间<img src=\"/images/time.png\"><time name=\"countDown\">" + SecondsToStr(tzTime) + "</time>");

                $(".js-djmoney").html("冻结：0");
                //$("#sscIssue").text(curIssue.IssueNum);
                $("[name='countDown']").text(SecondsToStr(tzTime));
                $("#SubBet").attr("onclick", "ShowBetConfig(false)");
                $(".js-fastbet").attr("onclick", "");
                //$("#btnChaseNum").unbind();
                //$("#btnChaseNum").click(ChaseNum);
            }
        }
    });
}

//获取当前开奖期号
function GetKJIssue(isInit) {
    //console.log("GetKJIssueS:" + new Date());
    isGettingKJIssue = 1;
    $.ajax({
        url: "/Uc/GetKJIssue?" + Math.random(),
        type: "post",
        dataType: "json",
        success: function (result) {
            isGettingKJIssue = 0;
            if (!result.success) {
                $.messager.alert("提示", result.msg);
                setTimeout('window.location.href = "/Home"', 2000);
                //console.error(result.msg);
                //setTimeout("GetIssue("+sscId+")",10000);
                return;
            } else {
                curKJIssue = result.list;
                $("#lastSscIssue").text(result.list.IssueNum);
                //$("#lastNumImg").prop("src", "images/avatar_0.png");//.text("正在开奖");
                kjTime = result.nTime;
                if (isInit)
                    GetLastNum(isInit);
                //console.log("GetKJIssueE:" + new Date());
            }
        },
        complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
            if (status == 'timeout') {//超时,status还有success,error等值的情况
                setTimeout(GetKJIssue, 1000);
            }
        }
    });
}

//获取历史开奖结果
function GetLastNum(isinit) {
    isinit = isinit ? true : false;
    $.ajax({
        url: "/Uc/GetLastNum?" + Math.random(),
        type: "post",
        dataType: "json",
        data: { "isinit": isinit },
        success: function (result) {
            if (!result.success) {
                console.error(result.msg);
                $.messager.alert("提示", result.msg);
                setTimeout('window.location.href = "/Home"', 2000);
            } else {
                if (result.list.length != null && result.list.length > 0) {
                    if (!result.list[0].status) {
                        //$("#lastNumImg").prop("src", "images/avatar_0.png");
                        StartLTimer();
                    } else {
                        //if ($(".js-lotteryinfo").find("li:contains('"+result.list[0].IssueNum+"')").length<1) {
                        //alert(curKJIssue.IssueNum + "_" + result.list[0].IssueNum);
                        //if (curKJIssue.IssueNum == result.list[0].IssueNum) {
                        if (result.list[0].status*1>0) {
                            lastResult = result;
                            lastLotteryNum = result.list[0].lotterynum;
                            if (isinit)
                                ShowLastNum();
                            //GetUserWinRanking();
                            //GetBetList();
                            //GetWinList(isinit);
                        }
                        isGettingNum = 0;
                    }
                }
            }
        }
    });
}
var lastResult;
function ShowLastNum() {
    if (lastLotteryNum == "1")
        num = 1;
    else if (lastLotteryNum == "2")
        num = 21;
    $("#coin" + num).show().siblings().hide();
    $(".js-lotteryinfo").find("li").remove();//.html("");
    for (var i = lastResult.list.length - 1; i >= 0; i--) {
        $(".js-lotteryinfo").prepend("<li>" + lastResult.list[i].issuenum + "&nbsp;&nbsp;&nbsp;" + BindLotteryNum(lastResult.list[i].lotterynum) + "</li>");
    }
}
//
var i = 1;
var auto;
var scrolling = false;
var joj = 1;//1是加速2是减速
var maxTimes;//最大速度持续次数
function StartScroll() {
    if (scrolling) return;
    scrolling = true;
    GetAllBet(true);
    lastLotteryNum = "0";
    num = 0;
    speed = 80;
    joj = 1;
    maxTimes = 5;
    startRoll();
    auto = setInterval(function () {
        if (joj == 1) {
            speed -= 10;
            if (speed <= 10) joj = 2;
        } else {
            if (maxTimes > 1) {
                maxTimes--;
                return;
            }
            if (num == 0) {
                if (speed < 70)
                    speed+=2;
            }
            else {
                if (speed < 80)
                    speed += 10;
            }
        }
    }, 500);
}
function startRoll() {
    i++;
    if (i <= 37) {
        $("#coin" + i).show().siblings().hide();
    } else {
        i = 1;
        $("#coin1").show().siblings().hide();
    }
    if (lastLotteryNum == "1")
        num = 1;
    else if (lastLotteryNum == "2")
        num = 21;
    if (num != i || speed < 80)
        setTimeout(startRoll, speed);
    else {
        scrolling = false;
        clearInterval(auto);
        ShowLastNum();
        GetUserInfo();
    }
}
function SecondsToStr(s) {
    //var h = parseInt(s / 3600);
    var m = parseInt(s/1000 % 3600 / 60);
    var ss = parseInt(s/1000 % 60);
    var hm ="000"+ parseInt(s % 1000);
    return (m < 10 ? "0" + m : m) + ":" + (ss < 10 ? "0" + ss : ss) + "." + hm.substr(hm.length-3,3);
}
var isGettingNum = 0;
var isGettingKJIssue = 0;
var isKJ = true;
var isPJ = true;
var isOver30 = true;
//倒计时
function FreshTime() {
    kjTime -= new Date().getTime() - lastTime.getTime();
    tzTime -= new Date().getTime() - lastTime.getTime();
    if (kjTime < 0) {
        if (isGettingKJIssue == 0) {
            GetKJIssue();
            if (isGettingNum == 0) {
                isGettingNum = 1;
                StartScroll();
                //lastLotteryNum = "0";
                GetLastNum();
            }
        }
    }
    if (tzTime >= 30000) {
        msg = "正在等待开奖，请稍候!";
        $("#SubBet").attr("onclick", "CancelBet();");
        $(".js-fastbet").attr("onclick", "CancelBet();");
        if (curIssue != null) {
            $("#BetInfo").html("第<b id=\"sscIssue\">" + (curIssue.IssueNum - 1) +
                "</b>期正在开奖, 剩余时间<img src=\"/images/time.png\"><time name=\"countDown\">" + SecondsToStr(tzTime - 30000 < 0 ? 0 : tzTime - 30000) + "</time>");
        } else {
            $("#BetInfo").html("第<b id=\"sscIssue\">正在加载</b>期正在开奖, 剩余时间<img src=\"/images/time.png\"><time name=\"countDown\">" + SecondsToStr(tzTime - 30000 < 0 ? 0 : tzTime - 30000) + "</time>");
        }
    }
    else if (tzTime <= 0) {
        //if (tzTime >= 31000 && isOver30) {
        //    //pjTime = tzTime - 30000 - 5000 - 20000;
        //    //ekjTime = tzTime - 35000 - 5000;
        //    //ztTime = tzTime - 55000;
        //    curIssue.IssueNum -= 1;
        //    isOver30 = false;
        //}
        $("#lastSscIssue").text(curIssue.IssueNum);
        msg = "正在开奖，请稍候!";
        $("#SubBet").attr("onclick", "CancelBet();");
        $(".js-fastbet").attr("onclick", "CancelBet();");
        $("#btnChaseNum").unbind();
        $("#btnChaseNum").click(CancelBet);
        //$("#BetInfo").html("<b id=\"sscIssue\">" + curIssue.IssueNum + "</b>期正在开奖<br>剩余时间<strong name=\"countDown\">" + SecondsToStr(ztTime < 0 ? 0 : ztTime / 1000) + "</strong>");
        
        if (ztTime <= 0) {
            if (ekjTime <= 0) {
                if (pjTime <= 0) {
                    if (isPJ) {
                        isPJ = false;
                        lastIssue = curIssue;
                        GetIssue();
                    }
                } else {
                    $("#iptz").val("0");
                    $("#iptf").val("0");
                    $("#BetInfo").html("第<b id=\"sscIssue\">" + curIssue.IssueNum + "</b>期正在派奖, 剩余时间<img src=\"/images/time.png\"><time name=\"countDown\">" + SecondsToStr(pjTime < 0 ? 0 : pjTime) + "</time>");
                    pjTime -= new Date().getTime() - lastTime.getTime();
                }
            } else {
                if (isKJ) {
                    isKJ = false;
                    StartScroll();
                }
                $("#BetInfo").html("第<b id=\"sscIssue\">" + curIssue.IssueNum + "</b>期正在开奖, 剩余时间<img src=\"/images/time.png\"><time name=\"countDown\">" + SecondsToStr(ekjTime < 0 ? 0 : ekjTime) + "</time>");
                ekjTime -= new Date().getTime() - lastTime.getTime();
            }
        } else {
            $("#BetInfo").html("第<b id=\"sscIssue\">" + curIssue.IssueNum + "</b>期停止投注, 暂停时间<img src=\"/images/time.png\"><time name=\"countDown\">" + SecondsToStr(ztTime < 0 ? 0 : ztTime) + "</time>");
            ztTime -= new Date().getTime() - lastTime.getTime();
        }
    }
    else {
        $("#SubBet").attr("onclick", "ShowBetConfig(false)");
        $(".js-fastbet").attr("onclick", "");
        ztTime = 5000;
        ekjTime = 20000;
        pjTime = 5000;
        isPJ = true;
        isKJ = true;
        isOver30 = true;
        $("#BetInfo").html("第<b id=\"sscIssue\">" + (curIssue.IssueNum) +
            "</b>期正在投注, 剩余时间<img src=\"/images/time.png\"><time name=\"countDown\">" + SecondsToStr(tzTime < 0 ? 0 : tzTime) + "</time>");
        //$("[name='countDown']").text(tzTime < 0 ? "正在加载..." : SecondsToStr(tzTime));
    }

    lastTime = new Date();
}
function StartLTimer() {
    setTimeout("GetLastNum()", 2000);
}

function SetBody() {
    //if (!browser.versions.android && !browser.versions.iPhone && !browser.versions.iPad) {
    //    var padding = ($(document).width() - 1200) / 2;
    //    if (padding < 0) padding = 0;
    //    $("body").attr("style", "padding-left:" + padding + "px;padding-right:" + padding + "px;background:#eff0f0;");
    //}
}
$(window).resize(function () {
    SetBody();
});


function BindLotteryNum(num) {
    if (num != "" && num != undefined) {
        num = num.replace("1", "正");
        num = num.replace("2", "反");
    }
    return num;
}
$(function () {
    //StartScroll();
    GetIssue();
    //GetBetList();
    GetKJIssue(true);
    setInterval(FreshTime, 1);
    GetAllBet();
    SetBody();
    $(".js-img").css("cursor", "pointer");
    $(".js-img").click(function () {
        $(".js-img").removeClass("js-img-check");
        $(this).addClass("js-img-check");
    });
});


